package com.deliinsns.sys.listener;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.deliinsns.sys.util.PropertyUtils;
public class InitBasicDataFromFile implements ServletContextListener {

	static Logger logger = Logger.getLogger(InitBasicDataFromFile.class.getName());
	public void contextInitialized(ServletContextEvent event) {
		//初始化系统,加载系统信息
		ServletContext servletContext = event.getServletContext();
		SpringContext.servletContext= servletContext;
		String configFilePath = servletContext.getRealPath("WEB-INF/config/config.properties");
		String codeDescFilePath = servletContext.getRealPath("WEB-INF/config/codeDesc.properties");
		Properties config= PropertyUtils.getProperties(configFilePath);
		Properties codeDesc= PropertyUtils.getProperties(codeDescFilePath);
		for(Enumeration e=config.propertyNames(); e.hasMoreElements();){  
		    String key=(String) e.nextElement();  
		    servletContext.setAttribute(key, config.getProperty(key));
		}  
		servletContext.setAttribute("codeDesc", codeDesc);
//		ApplicationContext context= SpringContext.getApplicationContext();
//		HibernateTemplate hibernateTemplate=(HibernateTemplate) context.getBean("hibernateTemplate");
//	      List baseDataList=hibernateTemplate.find("from BaseData");
//	      System.out.println("baseDataList.size:"+baseDataList.size());
		 // ApplicationContext context = BeanManager.getContext();
		 // super.contextInitialized(event);
		//BeanManager.= WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());
//		   // 总结：监听器在spring容器加载之前初始化，所以程序运行到这里时spring对象还没有被初始化，当然context为空咯！呵呵
		//  HibernateTemplate hibernateTemplate=(HibernateTemplate) context.getBean("hibernatetemplate");
	     // List baseDataList=hibernateTemplate.find("from BaseData");
	      //System.out.println("baseDataList.size:"+baseDataList.size());
//	      List basicInfo=hibernateTemplate.find("from Basicattr");//初始化workState
//	      System.out.println("大小："+basicInfo.size());
//				      List buildingList=hibernateTemplate.find("from Building");//初始化workState
//				      //List managerList=hibernateTemplate.find("from Manager");//初始化workState
//			          event.getServletContext().setAttribute("basicInfo",basicInfo );
//			          event.getServletContext().setAttribute("buildingList",buildingList );
//			         // event.getServletContext().setAttribute("managerList",managerList );
//			          event.getServletContext().setAttribute("sysState",1);

		 
		 
	}
	public void contextDestroyed(ServletContextEvent sce){}
	
}
